package com.qd.marketlist;

import android.net.Uri;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;


public class MainActivity extends Activity implements TabListener{
    private static final String TAG = "MainActivity";
    private static final boolean DBG = MLConst.DEBUG;

    private static final String TAB_STATE = "tab";
    private static final int ADDITEM_RESULT = 8087;
    
    private String mDBType = MLConst.DBSTR_TYPE_SUPERMARKET;
    private Uri mUri = MyProvider.CONTENT_URI;
    //private supermarketFrag mSMFragment = null;
    //private drugstoreFrag mDSFragment = null;

    private MyFragment mSMFragment = null;
    private MyFragment mDSFragment = null;

    private Context mContext = this;

    private void initHistoryDB(){
        Cursor c = mContext.getContentResolver().query(
                MyProvider.CONTENT_HISTORYURI, null, null, null, null);
        if(0 == c.getCount()){
            ContentValues values = new ContentValues();

    	    int[] sm_rids = {
    			    R.string.goods1,
    			    R.string.goods2,
    			    R.string.goods3,
    			    R.string.goods4,
    			    R.string.goods5,
    			    R.string.goods6,
    			    R.string.goods7,
    			    R.string.goods8,
    			    R.string.goods9
    	        };
    	    int i = sm_rids.length;
    	    for(int rid:sm_rids){
    	        values.put(MLConst.DBSTR_COL_NAME, getString(rid));
    	        values.put(MLConst.DBSTR_COL_COUNT, i--);
    		
    	        mContext.getContentResolver().insert(MyProvider.CONTENT_HISTORYURI, values);
    	    }
        }
        c = mContext.getContentResolver().query(
                MyProvider.CONTENT_DRUGHISTORY_URI, null, null, null, null);
        if(0 == c.getCount()){
            ContentValues values = new ContentValues();
    	    int[] ds_rids = {
    			    R.string.drug1,
    			    R.string.drug2,
    			    R.string.drug3,
    			    R.string.drug4,
    			    R.string.drug5,
    			    R.string.drug6,
    			    R.string.drug7,
    			    R.string.drug8,
    			    R.string.drug9
    	        };
    	    int i = ds_rids.length;
    	    for(int rid:ds_rids){
    		    values.put(MLConst.DBSTR_COL_NAME, getString(rid));
    		    values.put(MLConst.DBSTR_COL_COUNT, i--);

    		    mContext.getContentResolver().insert(MyProvider.CONTENT_DRUGHISTORY_URI, values);
    	    }
        }
        c.close();
    }
    private void initFragments(){

        mSMFragment = new MyFragment();
        mDSFragment = new MyFragment();

    }
    
    public String getCurrentTab(){
    	return mDBType;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFragments();

        initHistoryDB();

        ActionBar actionBar = getActionBar();

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
        //actionBar.setDisplayShowHomeEnabled(false);
        //actionBar.setDisplayShowTitleEnabled(false);
        
        //actionBar.setDisplayUseLogoEnabled(true);
        //actionBar.setLogo(R.drawable.icon);// addTab(actionBar.newTab().set setLogo(R.drawable.icon).setTabListener(this).setTag(MLConst.TAG_LOGO));

        actionBar.addTab(actionBar
                .newTab()
                .setText(R.string.supermarket)
                .setTabListener(this)
                .setTag(MLConst.TAG_SUPERMARKET));
        actionBar.addTab(actionBar
                .newTab()
                .setText(R.string.drugstore)
                .setTabListener(this)
                .setTag(MLConst.TAG_DRUGSTORE));

        if (savedInstanceState != null) {
            actionBar.setSelectedNavigationItem(savedInstanceState.getInt("TAB_STATE", 0));
        }
        
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(TAB_STATE, getActionBar().getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private DialogInterface.OnClickListener addInterface =  new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which){
            AlertDialog ad = (AlertDialog)dialog;
            

            EditText et = (EditText)ad.findViewById(R.id.ad_editview);
            String addString = et.getText().toString();
            if(addString.isEmpty()){
                
            }else{
                // DBHelper dbHelper = new DBHelper(mContext);
                ContentValues values = new ContentValues();
                values.put(MLConst.DBSTR_COL_NAME, addString);
                values.put(MLConst.DBSTR_COL_CHECKBOX_STATE, MLConst.DB_STATE_UNCHECKED);
                //dbHelper.getWritableDatabase().insert(MLConst.DB_TABLE_NAME, null, values);
                //if(mDBType.equals(MLConst.DBSTR_TYPE_SUPERMARKET)){
                //	mSMFragment.notifyDataChanged();
                //}else{
                //	mDSFragment.notifyDataChanged();
                //}
                //dbHelper.getWritableDatabase().close();
                mContext.getContentResolver().insert(mUri, values);
            }
        }
    };
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.menu_additem:{
            //LayoutInflater inflater = getLayoutInflater();
            //View layout = inflater.inflate(R.layout.add_dialog, null);
            //new AlertDialog.Builder(this)
            //    .setTitle(R.string.menu_add)
            //   .setView(layout)
            //    .setPositiveButton(R.string.menu_add, addInterface)
            //    .setNegativeButton(R.string.cancel, null)
            //    .show();
            if(DBG) Log.e(TAG, "start act " + mDBType);
        	startActivityForResult(
        			new Intent(this, AddItemActivity.class).putExtra(MLConst.KEY_CUR_TYPE, mDBType),
        			ADDITEM_RESULT);
            break;
        }
        default:
            break;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    	if(RESULT_OK == resultCode){    	
    	}
    }
    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
        
        switch ((Integer) tab.getTag()) {
        case MLConst.TAG_SUPERMARKET:{
            if(DBG) Log.e(TAG,"select tab supermarket");
            mDBType = MLConst.DBSTR_TYPE_SUPERMARKET;
            mUri = MyProvider.CONTENT_URI;
            ft.add(R.id.fragment_content, mSMFragment, mSMFragment.getClass().getName());
            break;
        }
        case MLConst.TAG_DRUGSTORE:{
            if(DBG) Log.e(TAG,"select tab drugstore");
            mDBType = MLConst.DBSTR_TYPE_DRUGSTORE;
            mUri = MyProvider.CONTENT_DRUG_URI;
            ft.add(R.id.fragment_content, mDSFragment, mDSFragment.getClass().getName());
            break;
        }
        default:
            break;
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        switch ((Integer) tab.getTag()) {
        case MLConst.TAG_SUPERMARKET:{
            ft.remove(mSMFragment);
            break;
        }
        case MLConst.TAG_DRUGSTORE:{
            ft.remove(mDSFragment);
            break;
        }
        default:
            break;
        }
    }

}
